using B.S.BaseData.Api.Read.Command.OrderCommand;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using MediatR;
using Microsoft.EntityFrameworkCore;
using WorkOrder.Domain;
using WorkOrder.Domain.DTOs;

namespace B.S.BaseData.Api.Read.CommandHandler.OrderCommandHandler
{
    /// <summary>
    /// 获取问诊记录处理器
    /// </summary>
    public class GetConsultationRecordHandler : IRequestHandler<GetConsultationRecordCommand, APIResult<ConsultationRecordDto>>
    {
        private readonly IBaseRepository<ConsultationOrder> _consultationOrderRepository;

        public GetConsultationRecordHandler(IBaseRepository<ConsultationOrder> consultationOrderRepository)
        {
            _consultationOrderRepository = consultationOrderRepository;
        }

        public async Task<APIResult<ConsultationRecordDto>> Handle(GetConsultationRecordCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 根据订单编号查询问诊单
                var order = await _consultationOrderRepository.GetAll()
                    .Where(x => x.OrderNumber == request.OrderNumber && !x.IsDelete)
                    .FirstOrDefaultAsync(cancellationToken);

                if (order == null)
                {
                    return new APIResult<ConsultationRecordDto>
                    {
                        Code = ApiEnum.Fail,
                        Msg = "问诊单不存在",
                        Data = null
                    };
                }

                // 检查是否有问诊记录
                if (!order.HasConsultationRecord)
                {
                    return new APIResult<ConsultationRecordDto>
                    {
                        Code = ApiEnum.Fail,
                        Msg = "该问诊单暂无问诊记录",
                        Data = null
                    };
                }

                // 构建问诊记录DTO
                var consultationRecord = new ConsultationRecordDto
                {
                    OrderNumber = order.OrderNumber,
                    PatientName = order.PatientName,
                    DoctorName = order.DoctorName,
                    Department = order.Department,
                    ConsultationDate = order.ConsultationStartTime ?? order.SubmitTime,
                    ConsultationMethod = order.ConsultationMethod,
                    SymptomDescription = order.SymptomDescription,
                    MedicalHistory = order.MedicalHistory,
                    Diagnosis = "根据症状分析，诊断为记忆力减退", // 这里可以根据实际业务逻辑生成诊断
                    TreatmentPlan = "建议进行记忆力训练，保持良好的作息习惯，必要时可考虑药物治疗", // 这里可以根据实际业务逻辑生成治疗方案
                    Prescription = order.OrderStatus == "已完成" ? "维生素B族、银杏叶提取物" : null, // 根据订单状态决定是否有处方
                    FollowUpAdvice = "建议1个月后复查，如有不适请及时就医",
                    ConsultationDuration = order.CallDurationMinutes ?? 0,
                    PatientRating = order.PatientRating,
                    PatientReview = order.PatientReview,
                    Notes = order.Remarks
                };

                return new APIResult<ConsultationRecordDto>
                {
                    Code = ApiEnum.Success,
                    Msg = "获取问诊记录成功",
                    Data = consultationRecord
                };
            }
            catch (Exception ex)
            {
                return new APIResult<ConsultationRecordDto>
                {
                    Code = ApiEnum.Fail,
                    Msg = $"获取问诊记录失败: {ex.Message}",
                    Data = null
                };
            }
        }
    }
} 